package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"mime/multipart"
	"trpg-base/dto"
	"trpg-base/entity"
	"trpg-common/request"
	"trpg-common/response"
	"trpg-common/utils/gorm_util"
)

type ForumHandler struct{}

func (ForumHandler) NewTopic(c *gin.Context) {
	topic := entity.ForumTopic{}
	request.GetStructFromGinContext(c, &topic)
	if topic.PicCount > 0 {
		topic.PicFiles = make([]*multipart.FileHeader, 0, int(topic.PicCount))
		for i := 0; i < int(topic.PicCount); i++ {
			name := fmt.Sprintf("pic%d", i)
			file, _ := c.FormFile(name)
			topic.PicFiles = append(topic.PicFiles, file)
		}
	}
	user := request.GetUserFromGinContext(c)
	forumService.NewTopic(topic, user)
	response.Success(c)
}

func (ForumHandler) SearchTopicList(c *gin.Context) {
	topicRequest := dto.ForumTopicRequest{}
	request.GetStructFromGinContext(c, &topicRequest)
	pageRequest := gorm_util.PageRequest{}
	pageRequest.Page = int32(request.GetIntFromQuery(c, "page"))
	pageRequest.PageSize = int32(request.GetIntFromQuery(c, "pageSize"))
	user := request.GetUserFromGinContext(c)
	response.SuccessWithData(c, forumService.SearchTopic(topicRequest, pageRequest, user))
}

func (ForumHandler) LikeTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.LikeTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) DisLikeTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.DislikeTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) CollectTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.CollectTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) CancelCollectTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.CancelCollectTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) LockTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.LockTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) UnLockTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.UnlockTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) DeleteTopic(c *gin.Context) {
	topicId := request.GetIntFromPath(c, "topicId")
	user := request.GetUserFromGinContext(c)
	forumService.DeleteTopic(topicId, user)
	response.Success(c)
}

func (ForumHandler) SearchContentList(c *gin.Context) {
	topicRequest := dto.ForumContentRequest{}
	request.GetStructFromGinContext(c, &topicRequest)
	pageRequest := gorm_util.PageRequest{}
	pageRequest.Page = int32(request.GetIntFromQuery(c, "page"))
	pageRequest.PageSize = int32(request.GetIntFromQuery(c, "pageSize"))
	user := request.GetUserFromGinContext(c)
	response.SuccessWithData(c, forumService.SearchContent(topicRequest, pageRequest, user))
}

func (ForumHandler) NewContent(c *gin.Context) {
	content := entity.ForumContent{}
	request.GetStructFromGinContext(c, &content)
	if content.PicCount > 0 {
		content.PicFiles = make([]*multipart.FileHeader, 0, int(content.PicCount))
		for i := 0; i < int(content.PicCount); i++ {
			name := fmt.Sprintf("pic%d", i)
			file, _ := c.FormFile(name)
			content.PicFiles = append(content.PicFiles, file)
		}
	}
	user := request.GetUserFromGinContext(c)
	forumService.NewContent(content, user)
	response.Success(c)
}

func (ForumHandler) LikeContent(c *gin.Context) {
	contentId := request.GetIntFromPath(c, "contentId")
	user := request.GetUserFromGinContext(c)
	forumService.LikeContent(contentId, user)
	response.Success(c)
}

func (ForumHandler) DisLikeContent(c *gin.Context) {
	contentId := request.GetIntFromPath(c, "contentId")
	user := request.GetUserFromGinContext(c)
	forumService.DislikeContent(contentId, user)
	response.Success(c)
}

func (ForumHandler) NewReply(c *gin.Context) {
	reply := entity.ForumReply{}
	request.GetStructFromGinContext(c, &reply)
	user := request.GetUserFromGinContext(c)
	forumService.NewReply(reply, user)
	response.Success(c)
}

func (ForumHandler) GetReplyList(c *gin.Context) {
	pageRequest := gorm_util.PageRequest{}
	request.GetStructFromGinContext(c, &pageRequest)
	contentId := request.GetIntFromQuery(c, "contentId")
	response.SuccessWithData(c, forumService.SearchReplyList(contentId, pageRequest))
}
